#include <stdio.h>
#include <string.h>
#if defined(TEST_TARGET_yaml)
#include <varch/command.h>
#include <varch/unitt.h>
#include <varch/yaml.h>
#else  
#include "init.h"
#include "command.h"
#include "unitt.h"
#include "kern.h"
#include "yaml.h"
#endif

/************************************************************************************/
/************************************* Unit Test ************************************/
/************************************************************************************/

// #define EXIT_TEST
extern uint64_t unitt_clock(void);

static int test_0(void)
{
    for (int i = 0; i < 100; i++)
    {
        if (0) 
        {
            
            #if defined (EXIT_TEST)
            exit(0);
            #endif 
            return UNITT_E_FAIL;
        }
    }
    
    return UNITT_E_OK;
}

static void unitt_task(void)
{
    static UNITT_TCASE rand_tests[] = {
        UNITT_TCASE(test_0),
        // UNITT_TCASE(test_1),
        // UNITT_TCASE(test_2),
    };

    static UNITT suites[] = {
        { "xxx suite", rand_tests, sizeof(rand_tests) / sizeof(rand_tests[0]) , unitt_clock },
    };

    UNITT_EXE(suites);
}

/************************************************************************************/
/************************************* Base Test ************************************/
/************************************************************************************/

#define READ_FILE "test/file/read.yaml"
#define WRITE_FILE "test/file/write.yaml"

static void yaml_preview(yaml_t yaml)
{
    char *s = yaml_dumps(yaml, 0, 0, YAML_F_NONE);
    if (s)
    {
        printf("%s\r\n", s);
        free(s);
    }
}

static void test_create(void)
{
    yaml_t yaml = yaml_create();
    if (yaml)
    {
        printf("yaml_create success!!! %p\r\n", yaml);
    }
    else 
    {
        printf("yaml_create fail!!!\r\n");
    }
    yaml_delete(yaml);
}

static void test_add(void)
{
    yaml_t yaml = NULL, info = NULL, hobby = NULL, key = NULL, complex = NULL;
    yaml = yaml_create();

    info = yaml_map_add_mapping(yaml, "Lamdonn", NULL);
    yaml_map_add_int(info, "age", 26);
    yaml_map_add_float(info, "height", 178.5);
    yaml_map_add_string(info, "email", "Lamdonn@163.com");

    yaml_set_alias(info, "Lamdonn", yaml);

    info = yaml_map_add_mapping(yaml, "Zhangsan", NULL);
    yaml_map_add_int(info, "age", 29);
    yaml_map_add_float(info, "height", 175);
    yaml_map_add_string(info, "email", "Zhangsan@163.com");

    hobby = yaml_map_add_sequence(info, "hobby", NULL);
    yaml_seq_add_string(hobby, "Playing basketball");
    yaml_seq_add_string(hobby, "Playing football");
    yaml_seq_add_string(hobby, "Playing mobile game");
    yaml_t kk = yaml_seq_add_mapping(hobby, NULL);
    yaml_map_add_int(kk, "year", 29);
    yaml_map_add_float(kk, "month", 6);

    info = yaml_map_add_mapping(yaml, "Lisi", NULL);
    complex = yaml_map_add_int(info, "1", 29);
    
    yaml_preview(yaml);

    yaml_delete(yaml);
}

static void test_insert(void)
{
    yaml_t root = NULL, doc = NULL, t = NULL;
    root = yaml_create();

    doc = yaml_insert_document(root, yaml_size(root), NULL);
    yaml_map_add_int(doc, "age", 26);
    yaml_map_add_float(doc, "height", 178.5);
    yaml_map_add_string(doc, "email", "Lamdonn@163.com");

    doc = yaml_insert_document(root, yaml_size(root), NULL);
    yaml_map_add_int(doc, "age", 26);
    yaml_map_add_float(doc, "height", 178.5);
    yaml_map_add_string(doc, "email", "Lamdonn@163.com");

    doc = yaml_insert_document(root, yaml_size(root), NULL);
    yaml_map_add_int(doc, "age", 26);
    yaml_map_add_float(doc, "height", 178.5);
    yaml_map_add_string(doc, "email", "Lamdonn@163.com");

    doc = yaml_insert_document(root, yaml_size(root), NULL);
    yaml_map_add_int(doc, "age", 26);
    yaml_map_add_float(doc, "height", 178.5);
    yaml_map_add_string(doc, "email", "Lamdonn@163.com");

    yaml_preview(root);

    yaml_delete(root);
}

static void test_remove(void)
{
    yaml_t yaml = NULL, info = NULL;
    yaml = yaml_create();

    info = yaml_map_add_mapping(yaml, "Lamdonn", NULL);
    yaml_map_add_int(info, "age", 26);
    yaml_map_add_float(info, "height", 178.5);
    yaml_map_add_string(info, "email", "Lamdonn@163.com");

    yaml_remove(info, "email", 0);
    
    yaml_preview(yaml);

    yaml_delete(yaml);
}

static void test_set(void)
{
    yaml_t yaml = NULL, info = NULL;
    yaml = yaml_create();

    info = yaml_map_add_mapping(yaml, "Lamdonn", NULL);
    yaml_map_add_int(info, "age", 26);
    yaml_map_add_float(info, "height", 178.5);
    yaml_map_add_string(info, "email", "Lamdonn@163.com");

    yaml_set_null(info); 
    yaml_preview(yaml);

    yaml_set_bool(info, YAML_TRUE); 
    yaml_preview(yaml);
    
    yaml_set_int(info, 123); 
    yaml_preview(yaml);

    yaml_set_float(info, 3.14159); 
    yaml_preview(yaml);

    yaml_set_string(info, "Hello yaml"); 
    yaml_preview(yaml);

    yaml_set_date(info, 2025, 01, 01);
    yaml_set_time(info, 16, 30, 03, 200);
    yaml_set_utc(info, +8, 30);
    yaml_preview(yaml);

    yaml_delete(yaml);
}

static void test_get(void)
{
    yaml_t yaml = NULL, info = NULL;
    yaml = yaml_create();

    info = yaml_map_add_mapping(yaml, "Lamdonn", NULL);

    yaml_t bool_ = yaml_map_add_bool(info, "open source", YAML_TRUE);
    printf("bool_: %d\r\n", yaml_value_bool(bool_));

    yaml_t int_ = yaml_map_add_int(info, "age", 26);
    printf("int_: %d\r\n", yaml_value_int(int_));

    yaml_t float_ = yaml_map_add_float(info, "height", 178.5);
    printf("float_: %f\r\n", yaml_value_float(float_));

    yaml_t string_ = yaml_map_add_string(info, "email", "Lamdonn@163.com");
    printf("string_: %s\r\n", yaml_value_string(string_));

    yaml_preview(yaml);

    yaml_delete(yaml);
}

static void test_key(void)
{
    yaml_t yaml = NULL, key = NULL;
    yaml = yaml_create();

    yaml_set_key(yaml, "StringKey");
    printf("key: %s\r\n", yaml_key(yaml));

    key = yaml_create();
    yaml_map_add_int(key, "a", 12);
    yaml_map_add_int(key, "b", 13);
    yaml_set_key_complex(yaml, key);
    printf("complex key: \r\n");
    yaml_preview(yaml_key_complex(yaml));

    yaml_preview(yaml);

    yaml_delete(yaml);
}

static void test_child(void)
{
    yaml_t yaml = NULL, info = NULL, key = NULL, child = NULL;
    yaml = yaml_create();

    info = yaml_map_add_mapping(yaml, "Lamdonn", NULL);
    yaml_map_add_int(info, "age", 26);
    yaml_map_add_float(info, "height", 178.5);
    yaml_map_add_string(info, "email", "Lamdonn@163.com");

    key = yaml_create();
    yaml_map_add_int(key, "a", 12);
    yaml_map_add_int(key, "b", 13);
    yaml_set_key_complex(yaml_map_add_string(info, "0", "This is a complex key"), key);

    child = yaml_get_child(info, "age", 0);
    printf("child['age']: %d\r\n", yaml_value_int(child));

    child = yaml_get_child(info, NULL, 1);
    printf("child[1]: %f\r\n", yaml_value_float(child));

    child = yaml_get_child_complex(info, key);
    printf("child[?]: %s\r\n", yaml_value_string(child));

    printf("child['email'] index = %u\r\n", yaml_get_index(info, "email", 0));
    printf("child[?] index = %u\r\n", yaml_get_index_complex(info, key));

    yaml_preview(yaml);

    yaml_delete(yaml);
}

static void test_alias(void)
{
    yaml_t yaml = NULL, info = NULL, temp = NULL;
    yaml = yaml_create();

    info = yaml_map_add_mapping(yaml, "Lamdonn", NULL);
    yaml_map_add_int(info, "age", 26);
    yaml_map_add_float(info, "height", 178.5);
    yaml_map_add_string(info, "email", "Lamdonn@163.com");

    yaml_set_alias(info, "AuthorInfo", yaml);

    printf("alias: %s\r\n", yaml_get_alias(info));

    yaml_preview(yaml);

    yaml_delete(yaml);
}

static void test_anchor(void)
{
    yaml_t yaml = NULL, info = NULL, temp = NULL;
    yaml = yaml_create();

    info = yaml_map_add_mapping(yaml, "Lamdonn", NULL);
    yaml_map_add_int(info, "age", 26);
    yaml_map_add_float(info, "height", 178.5);
    yaml_map_add_string(info, "email", "Lamdonn@163.com");

    yaml_set_alias(info, "AuthorInfo", yaml);

    for (int i = 0; i < yaml_anchor_size(yaml); i++)
    {
        yaml_t t = yaml_get_anchor(yaml, i);
        printf("anchor[%d]: %s\r\n", i, yaml_get_alias(t));
    }

    temp = yaml_map_add_mapping(yaml, "Something", NULL);
    yaml_set_anchor(temp, "AuthorInfo", yaml);

    temp = yaml_map_add_mapping(yaml, "Extended", NULL);
    yaml_insert_reference(temp, NULL, 0, "AuthorInfo", yaml);

    yaml_preview(yaml);

    yaml_delete(yaml);
}

static void test_copy(void)
{
    yaml_t yaml = NULL, info = NULL, copy = NULL;
    yaml = yaml_create();

    info = yaml_map_add_mapping(yaml, "Lamdonn", NULL);
    yaml_map_add_int(info, "age", 26);
    yaml_map_add_float(info, "height", 178.5);
    yaml_map_add_string(info, "email", "Lamdonn@163.com");
    printf("SOURCE yaml: \r\n");
    yaml_preview(yaml);

    printf("COPY yaml: \r\n");
    copy = yaml_copy(yaml, YAML_F_RECURSE);
    yaml_preview(copy);

    yaml_delete(copy);
    yaml_delete(copy);
}

static void test_compare(void)
{
    yaml_t yaml = NULL, info = NULL, temp = NULL, copy = NULL;
    yaml = yaml_create();

    info = yaml_map_add_mapping(yaml, "Lamdonn", NULL);
    yaml_map_add_int(info, "age", 26);
    yaml_map_add_float(info, "height", 178.5);
    yaml_map_add_string(info, "email", "Lamdonn@163.com");

    copy = yaml_copy(yaml, YAML_F_RECURSE);
    
    printf("yaml_compare %d\r\n", yaml_compare(yaml, copy, YAML_F_RECURSE));

    yaml_delete(copy);
    yaml_delete(copy);
}

static void test_dump(void)
{
    yaml_t root, node, temp;

    root = yaml_create();
    yaml_set_mapping(root, NULL);
    
    node = yaml_map_add_mapping(root, "mapping", NULL);
    yaml_map_add_string(node, "version", "1.0.0");
    yaml_map_add_string(node, "author", "Lamdonn");
    yaml_map_add_string(node, "license", "GPL-2.0");

    node = yaml_map_add_sequence(root, "sequence", NULL);
    yaml_seq_add_string(node, "file description");
    yaml_seq_add_string(node, "This is a C language version of yaml streamlined parser");
    yaml_seq_add_string(node, "Copyright (C) 2023 Lamdonn.");
    temp = yaml_seq_add_mapping(node, NULL);
    yaml_map_add_string(temp, "age", "18");
    yaml_map_add_string(temp, "height", "178cm");
    yaml_map_add_string(temp, "weight", "75kg");

    yaml_remove(temp, 0, 1);

    /* preview yaml */
    yaml_preview(root);

    /* dump yaml file */
    yaml_file_dump(root, WRITE_FILE);

    yaml_delete(root);
}

static void test_load(void)
{
    yaml_t root = NULL, x = NULL;
    
    root = yaml_file_load(READ_FILE, YAML_F_LDOCS);
    if (!root)
    {
        int type = 0, line = 0, column = 0;
        type = yaml_error_info(&line, &column);
        printf("error at line %d column %d type %d.\r\n", line, column, type);
        return;
    }
    printf("load success!\r\n");

    yaml_preview(root);

    yaml_delete(root);
}

static void test_base(void)
{
    // test_dump();
    test_load();
}

/************************************************************************************/
/*************************************  Command  ************************************/
/************************************************************************************/

static void usage(void)
{
    printf(
"Usage: yaml [opt] [arg] ...\n"
"\n"
"options:\n"
"    -e <execute>        Specifies the function to execute, the default is the <base> test\n"
"                        <base>      Test base function\n"
"                        <ut>        Unit test\n"
"                        <create>    Test create and delete functions\n"
"                        <add>       Test add category functions\n"
"                        <insert>    Test insert category functions\n"
"                        <remove>    Test remove category functions\n"
"                        <set>       Test set category functions\n"
"                        <get>       Test get category functions\n"
"                        <key>       Test key category functions\n"
"                        <child>     Test child category functions\n"
"                        <alias>     Test alias category functions\n"
"                        <anchor>    Test anchor category functions\n"
"                        <copy>      Test copy function\n"
"                        <compare>   Test compare function\n"
"                        <dump>      Test dump functions\n"
"                        <load>      Test load functions\n"
"    -h                  Print help\n"
"    -v                  Print version\n"
"    -u [<period>]       Unit test period, unit ms, the default is 1000ms\n"
"\n"
    );
}

static int test(int argc, char *argv[])
{
    char *execute = NULL;
    int ut_period = 1000;

    /* reset getopt */
    command_opt_init();

    while (1)
    {
        int opt = command_getopt(argc, argv, "e:hvu::");
        if (opt == -1) break;

        switch (opt) 
        {
        case 'u' :
            if (command_optarg) ut_period = atoi(command_optarg);
            break;
        case 'e' :
            execute = command_optarg;
            break;
        case 'v' :
            printf("yaml version %d.%d.%d\r\n", YAML_V_MAJOR, YAML_V_MINOR, YAML_V_PATCH);
            return 0;
        case '?':
            printf("Unknown option `%c`\r\n", command_optopt);
            return -1;
        case 'h' : 
        default:
            usage();
            return 0;
        }
    }

    if (execute)
    {
        if (!strcmp(execute, "base"))
        {
            test_base();
        }
        else if (!strcmp(execute, "ut"))
        {
            #if defined(TEST_TARGET_yaml)
            while (1)
            {
                unitt_task();
                usleep(1000 * ut_period);
            }
            #else  
            printf("create task %d\r\n", task_create(ut_period, unitt_task));
            #endif
        }
        else if (!strcmp(execute, "add"))
        {
            test_add();
        }
        else if (!strcmp(execute, "insert"))
        {
            test_insert();
        }
        else if (!strcmp(execute, "remove"))
        {
            test_remove();
        }
        else if (!strcmp(execute, "set"))
        {
            test_set();
        }
        else if (!strcmp(execute, "get"))
        {
            test_get();
        }
        else if (!strcmp(execute, "key"))
        {
            test_key();
        }
        else if (!strcmp(execute, "child"))
        {
            test_child();
        }
        else if (!strcmp(execute, "alias"))
        {
            test_alias();
        }
        else if (!strcmp(execute, "anchor"))
        {
            test_anchor();
        }
        else if (!strcmp(execute, "copy"))
        {
            test_copy();
        }
        else if (!strcmp(execute, "compare"))
        {
            test_compare();
        }
        else if (!strcmp(execute, "dump"))
        {
            test_dump();
        }
        else if (!strcmp(execute, "load"))
        {
            test_load();
        }
    }
    else  
    {
        test_base();
    }
    
    return 0;
}

/************************************************************************************/
/************************************ Test entry ************************************/
/************************************************************************************/

#if defined(TEST_TARGET_yaml)
int main(int argc, char *argv[])
{
    return test(argc, argv);
}
#else 
void test_yaml(void)
{
    command_export("yaml", test);
}
init_export_app(test_yaml);
#endif 
